<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
include_once("../config.inc.php");
session_start();

$arrOrderThrough = $_SESSION["OrderThrough"];
if (!is_array($arrOrderThrough)) {
	$arrOrderThrough[1]	= null;
	$arrOrderThrough[2]	= null;
	$arrOrderThrough[3]	= null;
	$arrOrderThrough[4]	= null;
	$arrOrderThrough[5]	= null;
	$arrOrderThrough[6]	= null;
}

$_LangPath = "mainthroughckeckout";	//語言檔參數，務必放在最前面
$intLID = (int)$_data["LottoID"];

include_once (__Language_Path."/selectlanguage_member.php");
include_once (__Member_Path."/logincheck_nodb.php");
include_once (__Common_Path."/ExchangeRate.inc.php");
include_once (__Common_Path."/Lotto.inc.php");
include_once (__Modules_Path."/joinnumber.lib.php");
include_once (__Common_Path."/limit/LottoLimit_". $intLID. ".inc.php");
include_once (__Common_Path."/betparam/BetParam_". $intLID. ".inc.php");
include_once (__Common_Path."/todaygame/Gamble".$intLID.".inc.php");
include_once (__Root_Path."/tmp/credit-". $_SESSION["Member_Info"] -> ID .".php");
include_once (__Common_Path. '/gambletype/GambleType1_'. $intLID. '.inc.php');
include_once (__Common_Path."/betparam/BetParam_".$intLID.".inc.php");
if($_SESSION["Member_Info"] -> isLock == '2'){
	$GLOBALS["errormessage"] = $_strStopWager;
	include("wager_error.php");
}
//找出 Apoint
$intAN = 0;
$_arrGambleType[14][0] -= abs($_arrBetParam[15][$intAN]['StakeJumpRate']);//正碼過關色波賠率比正碼色波小
$arrTOGap = explode(",", $_arrLotto[$intLID]["ThroughOddsGap"]);
$arrBTTOGap = array(12 => array($arrTOGap[0], $arrTOGap[0]), 13 => array($arrTOGap[1], $arrTOGap[1]), 14 => array($arrTOGap[3], $arrTOGap[4], $arrTOGap[5]));

$arrBT[1] = array(12, 13, 14);
$arrBT[5] = array(11, 12, 13);
//取得 TodayGame 資料
foreach($arrBT[$intLID] as $k => $intBT) {
	include (__Common_Path.'/todaygame/TodayGame'.$intLID.'_'. $intBT.'.inc.php');
}

//基本值
$OtherMixBetLimit	= '';
$OtherMaxEventBetLimit	= '';
$SpecialLimit		= '';
$StakeNumber		= '';
$intPreBT = ($intLID == 1)?15:14;

//判斷會員單注單場限額
$_SESSION[Member_Info]->arrBetLimit[$intLID][$intPreBT]	= empty($_SESSION[Member_Info]->arrBetLimit[$intLID][$intPreBT]) ? '0' : $_SESSION[Member_Info]->arrBetLimit[$intLID][$intPreBT];
$_SESSION[Member_Info]->arrEventLimit[$intLID][$intPreBT] = empty($_SESSION[Member_Info]->arrEventLimit[$intLID][$intPreBT]) ? '0' : $_SESSION[Member_Info]->arrEventLimit[$intLID][$intPreBT];

//找出公司最小單注
$flMemERate	= (empty($_ECRate[$_SESSION[Member_Info]->ERID][Rate])) ? 1 : $_ECRate[$_SESSION[Member_Info]->ERID][Rate];
$CompanyMixBet	= floor($_arrBetParam[$intPreBT][$intAN]["Com_BetLimit"] / $flMemERate);

//最低限額轉換匯率
$_arrBetParam[$intPreBT][$intAN][MinStake] = (empty($_arrBetParam[$intPreBT][$intAN][MinStake])) ? 0 : floor($_arrBetParam[$intPreBT][$intAN][MinStake] / $flMemERate);

//比對取出單注最低
$_arrLottoLimit[$intPreBT][0]	= (empty($_arrLottoLimit[$intPreBT][0])) ? 0 : floor($_arrLottoLimit[$intPreBT][0] / $flMemERate);
$_arrLottoLimit[$intPreBT][1]	= (empty($_arrLottoLimit[$intPreBT][1])) ? 0 : floor($_arrLottoLimit[$intPreBT][1] / $flMemERate);

$JoinMixBet = min($_SESSION[Member_Info]->arrBetLimit[$intLID][$intPreBT], $_arrLottoLimit[$intPreBT][1], $CompanyMixBet);

//剩餘信用額度
$_arrMemStake["_SUM"]	= ($_arrMemStake["_SUM"]) ? $_arrMemStake["_SUM"] : 0;
$CreditSurplus 		= $_SESSION["Member_Info"] -> CreditLine - $_arrMemStake["_SUM"];

//比對取出單場最高(取最小)
$JoinMaxEventBet = min($_SESSION[Member_Info] -> arrEventLimit[$intLID][$intPreBT] , $_arrLottoLimit[$intPreBT][0]);

//特殊單注單場限額(顯示用)
if ($_SESSION[Member_Info] -> arrBetLimit[$intLID][$intPreBT] != $JoinMixBet) {
	//另外單注限額
	$OtherMixBetLimit = '<font class="left_mt_pg_limit">'. $JoinMixBet .'</font>';
}

if ($_SESSION[Member_Info] -> arrEventLimit[$intLID][$intPreBT] != $JoinMaxEventBet) {
	//另外單場限額
	$OtherMaxEventBetLimit = '<font class="left_mt_pg_limit">'. $JoinMaxEventBet .'</font>';
}

//排序+做列表
if (!$_data[Number]) {
	$Err_Message = $_strMainThroughCkeckOut[Err_Message];
	$Err_HiddenMessage = $_strMainThroughCkeckOut[Err_Message];
	include("wager_error.php");
}

//GambleID
list($intGID, $arrGData) = each($_arrGamble);

$arrCounter[1] = array(1, 2, 3, 4, 5, 6);
$arrCounter[5] = array(1, 2, 3, 4);
$arrBTNum[1] = array(12 => array(0, 1), 13 => array(0, 1), 14 => array(0, 1, 2));
$arrBTNum[5] = array(11 => array(0, 1), 12 => array(0, 1), 13 => array(0, 1));
$arrBTPlusOdds[1] = array(12 => 1, 13 => 1, 14 => 0);
$arrBTPlusOdds[5] = array(11 => 1, 12 => 1, 13 => 1);
//賠率表
$intApointNum = 0;//$_arrGLineNumToApointNum[$intLID][12][$intNum];
foreach($arrBT[$intLID] as $k1 => $intBT){
	$intBTPOdds = $arrBTPlusOdds[$intLID][$intBT];
	$arrTGameBT = $_arrTodayGame[$intGID][$intBT];
	foreach($arrCounter[$intLID] as $k2 => $intCount){
		foreach($arrBTNum[$intLID][$intBT] as $k3 => $intBTNum){
			$intNum = $intCount*10 + $intBTNum;

			$OddsTable[$intBT][$intCount][$intBTNum] 	= ($arrTGameBT[$intNum]['isStopBet']) ? -1 : $intBTPOdds + $arrTGameBT[$intNum]['Odds'] + $_arrGambleType[$intBT][$intApointNum] + $arrBTTOGap[$intBT][$intBTNum];

			$OddsTableID[$intBT][$intCount][$intBTNum] 	= ($mainSingleOdds[$intCount] == -1) ? -1 : $arrTGameBT[$intNum]['OddsID'];
		}
	}
}

/*
for ($BetType_Counter = 1; $BetType_Counter <= 6; $BetType_Counter++) {
	$intApointNum = 0;//$_arrGLineNumToApointNum[$intLID][12][$intNum];

	$OddsTable[12][$BetType_Counter][0] 	= ($_arrTodayGame[$intGID][12][$BetType_Counter.'0'][isStopBet]) ? -1 : 1 + $_arrTodayGame[$intGID][12][$BetType_Counter.'0'][Odds] + $_arrGambleType[12][$intApointNum];
	$OddsTable[12][$BetType_Counter][1]	= ($_arrTodayGame[$intGID][12][$BetType_Counter.'1'][isStopBet]) ? -1 : 1 + $_arrTodayGame[$intGID][12][$BetType_Counter.'1'][Odds] + $_arrGambleType[12][$intApointNum];
	$OddsTable[13][$BetType_Counter][0] 	= ($_arrTodayGame[$intGID][13][$BetType_Counter.'0'][isStopBet]) ? -1 : 1 + $_arrTodayGame[$intGID][13][$BetType_Counter.'0'][Odds] + $_arrGambleType[13][$intApointNum];
	$OddsTable[13][$BetType_Counter][1] 	= ($_arrTodayGame[$intGID][13][$BetType_Counter.'1'][isStopBet]) ? -1 : 1 + $_arrTodayGame[$intGID][13][$BetType_Counter.'1'][Odds] + $_arrGambleType[13][$intApointNum];
	$OddsTable[14][$BetType_Counter][0] 	= ($_arrTodayGame[$intGID][14][$BetType_Counter.'0'][isStopBet]) ? -1 : $_arrTodayGame[$intGID][14][$BetType_Counter.'0'][Odds] + $_arrGambleType[14][$intApointNum];
	$OddsTable[14][$BetType_Counter][1] 	= ($_arrTodayGame[$intGID][14][$BetType_Counter.'1'][isStopBet]) ? -1 : $_arrTodayGame[$intGID][14][$BetType_Counter.'1'][Odds] + $_arrGambleType[14][$intApointNum];
	$OddsTable[14][$BetType_Counter][2] 	= ($_arrTodayGame[$intGID][14][$BetType_Counter.'2'][isStopBet]) ? -1 : $_arrTodayGame[$intGID][14][$BetType_Counter.'2'][Odds] + $_arrGambleType[14][$intApointNum];

	$OddsTableID[12][$BetType_Counter][0] 	= ($mainSingleOdds[$BetType_Counter] == -1) ? -1 : $_arrTodayGame[$intGID][12][$BetType_Counter.'0'][OddsID];
	$OddsTableID[12][$BetType_Counter][1] 	= ($mainDualOdds[$BetType_Counter] == -1) ? -1 : $_arrTodayGame[$intGID][12][$BetType_Counter.'1'][OddsID];
	$OddsTableID[13][$BetType_Counter][0] 	= ($mainLargeOdds[$BetType_Counter] == -1) ? -1 : $_arrTodayGame[$intGID][13][$BetType_Counter.'0'][OddsID];
	$OddsTableID[13][$BetType_Counter][1] 	= ($mainSmallOdds[$BetType_Counter] == -1) ? -1 : $_arrTodayGame[$intGID][13][$BetType_Counter.'1'][OddsID];
	$OddsTableID[14][$BetType_Counter][0] 	= ($mainRedOdds[$BetType_Counter] == -1) ? -1 : $_arrTodayGame[$intGID][14][$BetType_Counter.'0'][OddsID];
	$OddsTableID[14][$BetType_Counter][1] 	= ($mainBlueOdds[$BetType_Counter] == -1) ? -1 : $_arrTodayGame[$intGID][14][$BetType_Counter.'1'][OddsID];
	$OddsTableID[14][$BetType_Counter][2] 	= ($mainGreenOdds[$BetType_Counter] == -1) ? -1 : $_arrTodayGame[$intGID][14][$BetType_Counter.'2'][OddsID];
}
*/

//特殊限額顯示
if ($OtherMixBetLimit || $OtherMaxEventBetLimit) {
	$SpecialLimit = '  <tr class="left_mt_pg_sp_limit_title"><td colspan="2" class="left_mt_pg_sp_limit_title"><font color="#FFFF00">'. $_strMainThroughCkeckOut[SpecialLimitTitle] .'</font></td></tr>';
	if ($OtherMixBetLimit) {
		$SpecialLimit .= '<tr class="left_mt_pg_sp_limit_con"><td class="left_mt_pg_sp_limit_tip">'. $_strMainThroughCkeckOut[MinWagerStakeTitle] .'</td><td class="left_mt_pg_sp_limit_value">'. $OtherMixBetLimit .'</td></tr>';
	}
	if ($OtherMaxEventBetLimit) {
		$SpecialLimit .= '<tr class="left_mt_pg_sp_limit_con"><td class="left_mt_pg_sp_limit_tip">'. $_strMainThroughCkeckOut[MaxRoundLimitTitle] .'</td><td class="left_mt_pg_sp_limit_value">'. $OtherMaxEventBetLimit .'</td></tr>';
	}
}

//刪除值判斷
if ($_data['Action'] == 'del' && $_data['Number']) {
	//unset($arrOrderThrough[$_data['Number'][0]][$_data['Bet_Type']]);
	unset($arrOrderThrough[$_data['Number'][0]]);
} else {
	//下注號碼 $arrOrderThrough[正碼1-6][bet_type]->Object
	/*$arrOrderThrough[$_data['Number'][0]][$_data['Bet_Type']] -> Type	= $_data['Number'][1];
	$arrOrderThrough[$_data['Number'][0]][$_data['Bet_Type']] -> Odds	= $OddsTable[$_data['Bet_Type']][$_data['Number'][0]][$_data['Number'][1]] + $arrBTTOGap[$_data['Bet_Type']][$_data['Number'][1]];
	$arrOrderThrough[$_data['Number'][0]][$_data['Bet_Type']] -> OddsID	= $OddsTableID[$_data['Bet_Type']][$_data['Number'][0]][$_data['Number'][1]];
	$arrOrderThrough[$_data['Number'][0]][$_data['Bet_Type']] -> ChoiceName	= $_data['ChoiceName'];*/
	$arrOrderThrough[$_data['Number'][0]] -> Type	= $_data['Number'][1];
	$arrOrderThrough[$_data['Number'][0]] -> BetType	= $_data['Bet_Type'];
	$arrOrderThrough[$_data['Number'][0]] -> Odds	= $OddsTable[$_data['Bet_Type']][$_data['Number'][0]][$_data['Number'][1]] + $arrBTTOGap[$_data['Bet_Type']][$_data['Number'][1]];
	$arrOrderThrough[$_data['Number'][0]] -> OddsID	= $OddsTableID[$_data['Bet_Type']][$_data['Number'][0]][$_data['Number'][1]];
	$arrOrderThrough[$_data['Number'][0]] -> ChoiceName	= $_data['ChoiceName'];
}

//paser 下注內容
$GroupCounter	= 0;
$OddsMath	= 1;
$ChoiceStatus = '<table class="left_mt_pg_choice" border="0" cellpadding="0" cellspacing="0">
  		    <tr>
    		      <td class="left_mt_pg_choice_type">'. $_strMainThroughCkeckOut[MainThroughType] .'</td>
    		      <td class="left_mt_pg_choice_number">'. $_strMainThroughCkeckOut[MainThroughNumber] .'</td>
    		      <td class="left_mt_pg_choice_odds">'. $_strMainThroughCkeckOut[MainThroughOdds] .'</td>
    		      <td class="left_mt_pg_choice_del">'. $_strMainThroughCkeckOut[MainThroughDelete] .'</td>
  		    </tr>';

//foreach ($arrOrderThrough as $intCounter => $arrBTValue) {
foreach ($arrOrderThrough as $intCounter => $objBTValue) {
	//if (!is_array($arrBTValue))continue;

	//foreach ($arrBTValue as $intBT => $objBTValue) {
		if (!is_object($objBTValue))continue;
		$intBT = $objBTValue -> BetType;

		$arrOrderThrough[$intCounter] -> Odds = $OddsTable[$intBT][$intCounter][$objBTValue -> Type];
		$arrOrderThrough[$intCounter] -> OddsID = $OddsTableID[$intBT][$intCounter][$objBTValue -> Type];
		$strOddsColor = ($objBTValue -> Odds != $OddsTable[$intBT][$intCounter][$objBTValue -> Type])?" style='color: #FF0000; font-weight: 700'":"";

		$objBTValue -> Odds = $OddsTable[$intBT][$intCounter][$objBTValue -> Type];
		$objBTValue -> OddsID = $OddsTableID[$intBT][$intCounter][$objBTValue -> Type];

		$GroupCounter++;
		$ChoiceStatus .= '<tr class="left_mt_pg_choice_con"><td class="left_mt_pg_choice_type_con">'. $_strMainThroughCkeckOut['BetTypeTitle'.$intCounter] .'</td>';

		$ChoiceStatus .= '<td class="left_mt_pg_choice_number_con">';
		switch($intLID.'_'.$intBT) {
			case '1_12':
			case '5_11':
				if ($objBTValue -> Type == 0) {
					$ChoiceStatus .= $_strMainThroughCkeckOut[SingleTitle];
				} else {
					$ChoiceStatus .= $_strMainThroughCkeckOut[DualTitle];
				}
				break;

			case '1_13':
			case '5_12':
				if ($objBTValue -> Type == 0) {
					$ChoiceStatus .= $_strMainThroughCkeckOut[LargeTitle];
				} else {
					$ChoiceStatus .= $_strMainThroughCkeckOut[SmallTitle];
				}
				break;

			case '5_13':
				if ($objBTValue -> Type == 0) {
					$ChoiceStatus .= $_strMainThroughCkeckOut[SOddTitle];
				} else {
					$ChoiceStatus .= $_strMainThroughCkeckOut[SEvenTitle];
				}
				break;

			case '1_14':
				if ($objBTValue -> Type == 0) {
					$ChoiceStatus .= $_strMainThroughCkeckOut[RedTitle];
				} elseif ($objBTValue -> Type == 1) {
					$ChoiceStatus .= $_strMainThroughCkeckOut[BlueTitle];
				} else {
					$ChoiceStatus .= $_strMainThroughCkeckOut[GreenTitle];
				}
				break;
		}
		$ChoiceStatus .= '</td>';
		
		$intApointNum = 0;//$_arrGLineNumToApointNum[$intLID][12][$intNum];
		$OddsMath	= $OddsMath * $objBTValue -> Odds;
		$ChoiceStatus	.= '<td class="left_mt_pg_choice_odds_con"'.$strOddsColor.'>'. $objBTValue -> Odds .'</td><td class="left_mt_pg_choice_del_con"><a class="left_mt_pg_choice_del" target="leftFrame" href="'. $_SERVER["PHP_SELF"] .'?Action=del&UserID='. $_SESSION[Member_Info] -> ID .'&GambleID='. $intGID .'&Bet_Type='. $intBT .'&OddsID='. $objBTValue -> OddsID .'&Odds='. ($objBTValue -> Odds + $_arrGambleType[$intBT][$intApointNum]) .'&LottoID='. $intLID .'&Number='. $intCounter . $objBTValue -> Type .'" onClick="parent.mainFrame.ReSetChoiceNumber(\''. $objBTValue -> ChoiceName .'\')">X</a></td></tr>';

	//}
}

$ChoiceStatus .= '<tr class="left_mt_pg_choice_status"><td colspan="4" class="left_mt_pg_choice_status">'. $GroupCounter .'&nbsp;'. $_strMainThroughCkeckOut[LinkNumber] .'&nbsp;1</td></tr></table>';

if ($GroupCounter == 0) {
	$Err_Message = $_strMainThroughCkeckOut[Err_NoChoiceNumber];
	$Err_HiddenMessage = $_strMainThroughCkeckOut[Err_NoChoiceNumber];
	include("wager_error.php");
}

//取得最高派彩及未超過派彩的最高下注金額
$MaxPayOff	 = $_arrBetParam[$intPreBT][$intAN][MaxPayOff];	//最高派彩
$MaxStakePayOff = floor($MaxPayOff / $OddsMath);		//未超過派彩的最高下注金額

//取得 group money ... 剩餘信用額度 / 組數 ....再跟(會員,公司,系統,派彩的最高下注金額)比取最低
$ChoiceGroupMoney	= min($JoinMixBet , $CreditSurplus , $MaxStakePayOff);
$MinChoiceGroupMoney	= $_arrBetParam[$intPreBT][$intAN][MinStake];

//調整超過時畫面所要使用的 css
/*if ($GroupCounter > 4) {
	$bodyclass = '<body class="left_body">';
} else {
	$bodyclass = '<body class="left_body">';
}*/
//產生 CheckKeyValue
$arrCheckKey = explode(" ",microtime());
$CheckKeyValue1 = $arrCheckKey[0];
$CheckKeyValue2 = $arrCheckKey[1];

eval("\$Err_MaxStakeMoneyMessage = \"$_strMainThroughCkeckOut[Err_MaxStakeMoneyMessage]\";");
eval("\$Err_MinStakeMoneyMessage = \"$_strMainThroughCkeckOut[Err_MinStakeMoneyMessage]\";");
eval("\$Err_MaxStakePayOffMessage = \"$_strMainThroughCkeckOut[Err_MaxStakePayOffMessage]\";");
eval("\$Err_JoinMixBetMessage = \"$_strMainThroughCkeckOut[Err_JoinMixBetMessage]\";");

//ob_start("callback");
//include $_SERVER[DOCUMENT_ROOT] .'/membertemplate/mainthrough_checkout.html';
//ob_end_flush();
$fp = @file(__Membertemplate_Path."/mainthrough_checkout.html");
$fp = implode("", $fp);
$fp = setHTMLCharset($fp);

$fp = str_replace("{Account}", $_SESSION[Member_Info] -> Account, $fp);
$fp = str_replace("{Currency}", $_ECRate[$_SESSION[Member_Info] -> ERID][$_intLang], $fp);
$fp = str_replace("{CreditLine}", $_SESSION[Member_Info] -> CreditLine, $fp);
$fp = str_replace("{CreditAlreadyUse}", $_arrMemStake["_SUM"], $fp);
$fp = str_replace("{CreditSurplus}", $CreditSurplus, $fp);

$fp = str_replace("{MaxStakeMoney}", $ChoiceGroupMoney, $fp);
$fp = str_replace("{MinStakeMoney}", $_arrBetParam[$intPreBT][$intAN][MinStake], $fp);
$fp = str_replace("{MaxStakePayOff}", $MaxStakePayOff, $fp);
$fp = str_replace("{JoinMixBet}", $JoinMixBet, $fp);
$fp = str_replace("{GroupCounter}", $GroupCounter, $fp);

$fp = str_replace("{MinWagerStake}", $_SESSION[Member_Info] -> arrBetLimit[$intLID][$intPreBT], $fp);
$fp = str_replace("{MaxRoundLimit}", $_SESSION[Member_Info] -> arrEventLimit[$intLID][$intPreBT], $fp);
$fp = str_replace("{SpecialLimit}", $SpecialLimit, $fp);

$fp = str_replace("{ChoiceStatus}", $ChoiceStatus, $fp);
$fp = str_replace("{Periods}", $arrGData[SerialNo], $fp);
$fp = str_replace("{MaxPayOff}", $MaxPayOff, $fp);

$fp = str_replace("{GambleID}", $_data[GambleID], $fp);
$fp = str_replace("{LottoID}", $intLID, $fp);
$fp = str_replace("{UserID}", $_SESSION[Member_Info] -> ID, $fp);
$fp = str_replace("{CheckKeyValue1}", $CheckKeyValue1, $fp);
$fp = str_replace("{CheckKeyValue2}", $CheckKeyValue2, $fp);
$fp = str_replace("{sid}", $_data[sid], $fp);
$fp = str_replace("{OddsID}", $_data[OddsID], $fp);
$fp = str_replace("{Odds}", $_data[Odds], $fp);
$fp = str_replace("{Number}", $_data[Number], $fp);
$fp = str_replace("{ChoiceName}", $_data[ChoiceName], $fp);
$fp = str_replace("{Bet_Type}", $intPreBT, $fp);
$fp = str_replace("{preBT}", $intPreBT, $fp);
$fp = str_replace("{ShowStakeMoney}", $_data[StakeMoney], $fp);

$fp = str_replace("{Err_MaxStakeMoneyMessage}", $Err_MaxStakeMoneyMessage, $fp);
$fp = str_replace("{Err_MinStakeMoneyMessage}", $Err_MinStakeMoneyMessage, $fp);
$fp = str_replace("{Err_MaxStakePayOffMessage}", $Err_MaxStakePayOffMessage, $fp);
$fp = str_replace("{Err_JoinMixBetMessage}", $Err_JoinMixBetMessage, $fp);

while(list($k, $v) = each($_strMainThroughCkeckOut)){
	$fp = str_replace("{".$k."}", $v, $fp);
}

$_SESSION["OrderThrough"] = $arrOrderThrough;
//輸出
WebOutputHTML($fp, 1);
?>